﻿using famails.bus;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.page
{
    public partial class DomainLimit : System.Web.UI.Page
    {
        static log4net.ILog logs = log4net.LogManager.GetLogger("ErrorRollingLogFileAppender");
        static UserLoginDTO userLogin = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                MailConfigBUS ctBUS = new MailConfigBUS();
                DataTable T = ctBUS.GetDomainLimit();
                if (T != null && T.Rows.Count > 0)
                {
                    decimal temp = 0;
                    decimal.TryParse(T.Select("Action_0Warning_1Ban=1")[0]["LimitFrom"] + "", out temp);
                    txtBanFrom.Text = temp.ToString("##,#");
                    txtBanRatio.Text = T.Select("Action_0Warning_1Ban=1")[0]["Ratio"] + "";


                    decimal.TryParse(T.Select("Action_0Warning_1Ban=0")[0]["LimitTo"] + "", out temp);
                    txtWarningFrom.Text = temp.ToString("##,#");
                    txtWarningRatio.Text = T.Select("Action_0Warning_1Ban=0")[0]["Ratio"] + "";
                }
            }
        }

        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<DomainLimitResult> Search(string domain, string from, string to, decimal top)
        {
            //string name = "", phone = "h", email = ""; int group = -1;
            MailConfigBUS ctBUS = new MailConfigBUS();
            DataTable customerBySelect = ctBUS.GetDomainStatus(domain, from, to, top);
            List<DomainLimitResult> result = new List<DomainLimitResult>();
            DomainLimitResult item = new DomainLimitResult();
            foreach (DataRow dr in customerBySelect.Rows)
            {
                item = new DomainLimitResult();
                item.DomainName = dr["DomainName"] + "";
                item.TotalSend = Convert.ToDecimal(dr["TotalSend"] + "");
                item.TotalDelivery = Convert.ToDecimal(dr["TotalDelivery"] + "");
                item.TotalBounce = Convert.ToDecimal(dr["TotalBounce"] + "");
                item.TotalError = Convert.ToDecimal(dr["TotalError"] + "");
                item.DeliveryPer = Convert.ToDouble(dr["DeliveryPer"] + "");
                item.BouncePer = Convert.ToDouble(dr["BouncePer"] + "");
                item.ErrorPer = Convert.ToDouble(dr["ErrorPer"] + "");
                result.Add(item);
            }
           /* List<object> result = new List<object>();
            string[] item = new string[5];
            item[0] = "Genre";
            item[1] = "Delivery";
            item[2] = "Error";
            item[3] = "Bounce";
            item[4] = "{ role: 'annotation' }";
            result.Add(item);

            item = new string[5];
            item[0] = "betraning.org";
            item[1] = "100";
            item[2] = "200";
            item[3] = "300";
            item[4] = "";
            result.Add(item);

            item = new string[5];
            item[0] = "famails.com";
            item[1] = "2";
            item[2] = "3";
            item[3] = "4";
            item[4] = "";
            result.Add(item);*/

            return result;
        }
    }
    public class DomainLimitResult
    {
        public string DomainName { get; set; }
        public decimal TotalSend { get; set; }
        public decimal TotalError { get; set; }
        public decimal TotalBounce { get; set; }
        public decimal TotalDelivery { get; set; }
        public double ErrorPer { get; set; }
        public double BouncePer { get; set; }
        public double DeliveryPer { get; set; }

    }
}